package com.lhj.demo.simple.service.impl;

import com.lhj.demo.simple.domain.TestEntity;
import com.lhj.demo.simple.mapper.TestMapper;
import com.lhj.demo.simple.service.TestOneService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hj.luo
 * @Date 2023-07-10 17:51
 * @descripition
 * @version: 1.0
 */
@Service
public class TestOneServiceImpl implements TestOneService {
    @Autowired
    TestMapper testMapper;
    @Lazy
    @Autowired
    TestOneService testOneService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDB(Integer id) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id);
        testEntity.setName("");
        int insert = testMapper.insert(testEntity);
        System.out.println(testEntity);
        this.saveDB2(id + 100000);
        // throw new RuntimeException("ssss");

    }

    private void saveDB2(Integer id) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id);
        testEntity.setName("");
        testMapper.insert(testEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveDB3(Integer id) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id + 200000);
        testEntity.setName("");
        testMapper.insert(testEntity);
    }

    @Override
    public void saveNotWithHas(Integer id) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id);
        testEntity.setName("");
        testMapper.insert(testEntity);
        ((TestOneServiceImpl) AopContext.currentProxy()).saveDB3(id); // 生效
        // 不生效
        // testOneService.saveDB3(id);
        throw new RuntimeException("DIY ERROR");
    }
}
